package DynamicProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * The `LoggingHandler` class is an implementation of the `InvocationHandler` interface,
 * designed to provide logging functionality for method invocations on a target object.
 * This class is typically used in conjunction with Java's dynamic proxy mechanism to
 * intercept and log method calls before and after their execution.
 *
 * The `LoggingHandler` logs the start and completion of each method invocation by
 * printing messages to the standard output. This can be useful for debugging or
 * monitoring the behavior of method calls in a dynamic proxy context.
 *
 * @param target The object whose method invocations are to be logged. This object is
 *               passed to the constructor and is the target of the method calls
 *               intercepted by this handler.
 *
 * @see InvocationHandler
 */
public class LoggingHandler implements InvocationHandler {
    private final Object target;

    public LoggingHandler(Object target) {
        this.target = target;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("LoggingHandler: 方法 " + method.getName() + " 开始执行");
        Object result = method.invoke(target, args);
        System.out.println("LoggingHandler: 方法 " + method.getName() + " 执行完成");
        return result;
    }
}
